#include "client.h"
int main(int argc, char *argv[]){   

    ARGS_CHECK(argc,3);
    int sockfd = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[2]));
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    ERROR_CHECK(ret,-1,"connect failed:");

    int epfd = epoll_create(1);

    //监听键盘输入
    struct epoll_event event;
    event.events = EPOLLIN;
    event.data.fd = STDIN_FILENO;
    epoll_ctl(epfd,EPOLL_CTL_ADD,STDIN_FILENO,&event);

    char buf[4096];
    struct epoll_event readyset[4096];

    while(1){
        int readynum = epoll_wait(epfd,readyset,4096,-1);
        for(int i=0;i<readynum;++i){
            printf("stdin success\n");
            if(readyset[i].data.fd == STDIN_FILENO){
                bzero(buf,sizeof(buf));
                read(STDIN_FILENO,buf,sizeof(buf));
                char lineValue[4096];
                bzero(lineValue,sizeof(lineValue));
                //短命令的传输
                //返回值type是命令类型，把两个长命令单独拿出来
                int ret = commandManage(buf,sockfd,lineValue);
                //上传
                if(ret == 21 || ret == 25){
                    longCommandManage(argv[1], ret);
                }

            }
        }
    }
    return 0;
}
